/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bsc.war.form;

import bsc.ejb.metadata.Colorimetry;
import bsc.ejb.metadata.Framework;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta
 */
public class NewColorimetry extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ModelAndView mav = new ModelAndView("renderXml");

        try {
            boolean edit = false;
            Long colorimetryId = -1L;

            if (NumberUtils.isDigits(request.getParameter("id"))) {
                Long id = Long.valueOf(request.getParameter("id"));
                if (id >= 0) {
                    edit = true;
                    colorimetryId = id;
                }

            }

            HttpSession session = request.getSession();
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

            Framework framework = null;
            if (session.getAttribute("frameworkId") instanceof Long) {

                Long frameworkId = (Long) session.getAttribute("frameworkId");
                framework = (Framework) Factory.getBean("Framework");
                framework = delegate.findFramework(Long.valueOf(frameworkId));

            } else {
                throw new Exception("no hay frameworkId");
            }


            //int priority = 0;
            //for(int i=0; i<=max;i++){

            String name = request.getParameter("name");
            String description = request.getParameter("description");
            String color = request.getParameter("color");

            Integer priority = 0;
            if (NumberUtils.isDigits(request.getParameter("priority"))) {
                priority = Integer.valueOf(request.getParameter("priority"));
            }

            Colorimetry col = null;
            if (!StringUtils.isEmpty((name))) {
                if (edit) {
                    col = delegate.findColorimetry(colorimetryId);
                } else {
                    col = (Colorimetry) Factory.getBean("Colorimetry");
                }

                col.setName(name);
                col.setDescription(description);
                col.setColor(color);
                col.setPriority(priority);
                col.setFramework(framework);

                if (edit) {
                    delegate.updateColorimetry(col);
                } else {
                    delegate.createColorimetry(col);
                }
            }

        //"frameworkId", framework.getIdFramework()
            if (col != null) {
                mav.addObject("id", col.getIdColorimetry());
            }
        } catch (Exception e) { }

        return mav;

    }
}
